package com.myapp.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import java.util.Set;



public class PreferencesUtil {
    private static PreferencesUtil sPreferences;
    private final SharedPreferences mSharedPreferences;
    private final SharedPreferences.Editor mEditor;

    public static final String USER_ID_CURRENT = "current_userId";
    public static final String USER_ID_SETS = "userIdSets";
    public static final String CURRENT_PHONE_ID = "current_phoneId";

    private PreferencesUtil(Context context){
        mSharedPreferences = context.getSharedPreferences(context.getPackageName(),Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public static synchronized PreferencesUtil getInstance(Context context){
        if (null == sPreferences){
            sPreferences = new PreferencesUtil(context.getApplicationContext());
        }
        return sPreferences;
    }

    public void putInt(final String key, final int value) {
        mEditor.putInt(key, value).apply();

      //  SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor);
    }

    public void putLong(final String key, final long value) {
        mEditor.putLong(key, value).apply();
     //   SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor);
    }

    public void putString(final String key, final String value) {
        mEditor.putString(key, value).apply();
    //    SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor);
    }

    public void putFloat(final String key, final float value) {
        mEditor.putFloat(key, value).apply();
    //    SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor);
    }

    public void putBoolean(final String key, final boolean value) {
        mEditor.putBoolean(key, value).apply();
    //    SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void putStringSet(final String key, final Set<String> values) {
        mEditor.putStringSet(key, values).apply();
    //    SharedPreferencesCompat.EditorCompat.getInstance().apply(mEditor);
    }

    public int getInt(final String key, final int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    public long getLong(final String key, final long defaultValue) {
        return mSharedPreferences.getLong(key, defaultValue);
    }

    public String getString(final String key, final String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    public float getFloat(final String key, final float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    public boolean getBoolean(final String key, final boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public Set<String> getStringSet(final String key) {
        return mSharedPreferences.getStringSet(key, null);
    }


}
